package service;

import dao.ResourceDao;
import dao.RoleAssResourceDao;
import domin.Resource;
import domin.RoleAssResource;
import exception.tsglException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public final class ResourceService {
	private static final ResourceDao resourceDao = ResourceDao.getInstance();
	private static final ResourceService resourceService = new ResourceService();


	public static ResourceService getInstance() {
		return resourceService;
	}

	public Collection<Resource> findAll(Integer roleId) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		Collection<RoleAssResource> roleAssResources = RoleAssResourceDao.getInstance().findAll(connection, roleId);
		JdbcHelper.close(connection);
		Collection<Resource> resources = new TreeSet<Resource>();
		for (RoleAssResource roleAssResource : roleAssResources){
			resources.add(roleAssResource.getResource());
		}
		return resources;
	}

	public Collection<Resource> findAll(Pagination pagination, List<Condition> conditionList) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		Collection<Resource> resources = resourceDao.findAll(connection, pagination, conditionList);
		JdbcHelper.close(connection);
		return resources;

	}

	public Resource find(Integer id) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		Resource resource = resourceDao.find(connection, id);
		JdbcHelper.close(connection);
		return resource;
	}
}
